package com.wang.cloud.common.auth.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpLogic;
import com.wang.cloud.common.auth.util.StpKit;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 对token自动续签
 *
 * @author wang ye
 * @date 2024/6/4
 */
public class AutoRenewConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handler -> {
                    StpLogic stpLogic = StpKit.getStpLogic();
                    // 判断当前会话是否已经登录
                    if (stpLogic.isLogin()) {
                        // 续签当前Token：(将 [最后操作时间] 更新为当前时间戳)
                        stpLogic.updateLastActiveToNow();
                    }
                }))
                .addPathPatterns("/v1/**");
    }
}
